<template>
  <el-dialog
    v-dialogDrag
    :title="dialogTitle"
    :visible="dialogVisible"
    class="dialogStyleSmall"
    :before-close="cancel"
  >
    <el-button style="margin-bottom: 10px;" size="small" @click="downTemp">
      下载模板
    </el-button>
    <!-- <el-upload
      ref="upload"
      class="upload-demo"
      :on-success="onUploadSuccess"
      :on-change="handleChange"
      :action="BASE_API"
      :headers="myHeaders"
      :with-credentials="true"
      :auto-upload="false"
      :limit="1"
      accept=".xlsx,.xls"
    >
      <el-button size="small" >点击上传</el-button>
      <div slot="tip" class="el-upload__tip">且不超过500kb</div>
    </el-upload> -->
    <file-uplode ref="fileUplode" style="margin-top:10px;" :edit-file-list="editFileList" :category="category" @getFileIdsList="getFileIdsList" />
    <template slot="footer">
      <el-button @click="downLoad">保 存</el-button>
      <el-button @click="cancel">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { userExport } from '@/api/system/user'
import FileUplode from '@/components/FileUplode/FileUplodeNoTable'
import { selectFileById } from '@/api/file/file.js'
export default {
  name: 'Export',
  components: { FileUplode },
  data() {
    return {
      dialogTitle: '',
      dialogVisible: false,
      /** 文件上传 *******start*/
      BASE_API: process.env.VUE_APP_BASE_API + '/sys/user/export',
      // 上传文件的请求头
      myHeaders: { 'token': this.$store.getters.token },
      fileName: '',
      templateName: '人员信息模板.xlsx',
      isTemp: false,
      category: 'userExport',
      editFileList: [],
      // 用于保存附件上传返回的fileIds(Sting逗号分隔)
      fileIdsString: '',
      form: {
        loginName: '',
        userName: '',
        phonenumber: '',
        status: '',
        dateRange: '',
        fileId: ''
      }
    }
  },
  methods: {
    openExport(form) {
      this.dialogTitle = '导出数据'
      this.dialogVisible = true
      this.form.loginName = form.loginName
      this.form.userName = form.userName
      this.form.phonenumber = form.phonenumber
      this.form.dateRange = form.dateRange
      this.form.status = form.status
    },
    /** ************ 附件上传 start*******************************************************/
    // 文件上传成功的回调
    onUploadSuccess(res, file) {
      if (res) {
        this.fileName = res.data.message
      } else {
        this.msgError(res.message)
      }
    },
    handleChange(file) {
      const _this = this
      if (file.status !== 'success') {
        this.$nextTick(() => {
          _this.$refs.upload.submit()
        })
      }
    },
    // 下载模板
    downTemp() {
      this.downloadUser(this.templateName, false)
    },
    // 对话框取消按钮
    cancel() {
      this.dialogVisible = false
      this.$refs.fileUplode.emptyFile()
      this.$refs.fileUplode.cancelDeleteByID()
    },
    downLoad() {
      // if (this.fileName !== '') {
      //   this.download(this.fileName)
      // }
      userExport(this.form).then(resp => {
        if (resp) {
          // this.download(resp.data)
          this.downloadUser(resp.data, true)
          this.$refs.fileUplode.emptyFile()
          this.$refs.fileUplode.cancelDeleteByID()
        } else {
          this.msgError(resp.message)
        }
      })
      this.dialogVisible = false
    },
    selectFildListByIds() {
      const _this = this
      const fileIds = this.form.fileId
      selectFileById(fileIds).then(resp => {
        if (resp) {
          const introductionListLook = resp.data
          introductionListLook.map(item => {
            const fileData = {}
            const fileId = item.fileId
            const fileName = item.name
            const url = item.path
            fileData.fileId = fileId
            fileData.name = fileName
            fileData.url = url
            _this.editFileList.push(fileData)
          })
        } else {
          _this.msgError(resp.message)
        }
      })
    },
    // 接受附件子组件 回传过来的附件id
    getFileIdsList(fileIdsList) {
      const _this = this
      let attachId = ''
      for (let i = 0; i < fileIdsList.length; i++) {
        attachId += fileIdsList[i].fileId + ','
      }
      // 去掉最后一个逗号
      if (attachId.length > 0) {
        attachId = attachId.substr(0, attachId.length - 1)
      }
      _this.fileIdsString = attachId
      this.form.fileId = this.fileIdsString
    }
  }
}
</script>

<style>

</style>
